package com.xyy.saas.payment.config;

import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.xyy.saas.payment.util.ApolloUtil;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author wangtianqi
 * @date 2019/12/26 18:06
 */

@Configuration
@DependsOn("apolloUtil")
public class DataSourceConfig{

    @Autowired
    private DataSourceProperties basicProperties;

    @Value("${spring.datasource.druid.maxActive}")
    private String maxActive;

    @Value("${spring.datasource.druid.initialSize}")
    private String initialSize;

    @Value("${spring.datasource.druid.maxWait}")
    private String maxWait;

    @Value("${spring.datasource.druid.minIdle}")
    private String minIdle;

    @Value("${spring.datasource.druid.timeBetweenEvictionRunsMillis}")
    private String timeBetweenEvictionRunsMillis;

    @Value("${spring.datasource.druid.minEvictableIdleTimeMillis}")
    private String minEvictableIdleTimeMillis;

    @Value("${spring.datasource.druid.validationQuery}")
    private String validationQuery;

    @Value("${spring.datasource.druid.testWhileIdle}")
    private String testWhileIdle;

    @Value("${spring.datasource.druid.testOnBorrow}")
    private String testOnBorrow;

    @Value("${spring.datasource.druid.testOnReturn}")
    private String testOnReturn;

    private String masterUserName = ApolloUtil.getProperty("mysql.master.username");
    private String masterPassword = ApolloUtil.getProperty("mysql.master.password");
    private String masterPublicKey =  ApolloUtil.getProperty("mysql.master.publicKey");

    private static Properties properties = new Properties();

    private void buildProperties(){
        if(properties.size() != 0) {
            return;
        }
        properties.setProperty("druid.maxActive", maxActive);
        properties.setProperty("druid.initialSize", initialSize);
        properties.setProperty("druid.minIdle", minIdle);
        properties.setProperty("druid.timeBetweenEvictionRunsMillis", timeBetweenEvictionRunsMillis);
        properties.setProperty("druid.minEvictableIdleTimeMillis", minEvictableIdleTimeMillis);
        properties.setProperty("druid.validationQuery", validationQuery);
        properties.setProperty("druid.testWhileIdle", testWhileIdle);
        properties.setProperty("druid.testOnBorrow", testOnBorrow);
    }


    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() throws Exception {
        basicProperties.setUsername(masterUserName);
        basicProperties.setPassword(ConfigTools.decrypt(masterPublicKey, masterPassword));
        buildProperties();
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        dataSource.setConnectProperties(properties);
        dataSource.setMaxWait(Long.parseLong(maxWait));
        dataSource.setTestOnReturn(Boolean.parseBoolean(testOnReturn));
        return dataSource;
    }



}
